package cn.mandifal.manager.controller;

import cn.mandifal.manager.service.GoodsManagementService;
import cn.mandifal.pojo.DpGoods;
import cn.mandifal.pojo.DpItem;
import cn.mandifal.pojo.GoodsCombine;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import entity.PageResult;
import entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.jms.*;
import java.util.List;

/**
 * 平台商品管理控制层
 * @author shun
 */
@RestController
@RequestMapping("/goodsManagementController")
public class GoodsManagementController {
    @Reference
    private GoodsManagementService goodsManagementService;
    @Autowired
    private JmsTemplate jmsTemplate;
    @Autowired
    private Destination queueSolrDestination;
    @Autowired
    private Destination queueSolrDeleteDestination;
    @Autowired
    private Destination topicPageDestination;
    @Autowired
    private  Destination topicPageDeleteDestination;


    /**
     * 搜索 spu
     *
     * @param pageNum  页码
     * @param pageSize 页面大小
     * @param dpGoods  商品 spu 实体
     * @return 返回分页结果
     */
    @RequestMapping("/search")
    public PageResult search(int pageNum, int pageSize, @RequestBody DpGoods dpGoods) {
        return goodsManagementService.search(pageNum, pageSize, dpGoods);
    }

    /**
     * 查询商品及详情
     *
     * @param id 商品 id
     * @return 返回组合实体
     */
    @RequestMapping("/findOne")
    public GoodsCombine findOne(Long id) {
        return goodsManagementService.findOne(id);
    }

    /**
     * 更新状态
     *
     * @param ids    商品 id
     * @param status 状态
     * @return 返回结果
     */
    @RequestMapping("/updateAuditStatus")
    public Result updateAuditStatus(@RequestBody Long[] ids, String status) {
        try {
            goodsManagementService.updateAuditStatus(ids, status);
            //如果审核通过则更新 solr 库和生成商品详情页
            if ("2".equals(status)) {
                //sku的状态 1 为上架，2 为下架，3 为删除
                List<DpItem> itemList = goodsManagementService.findItemListByGoodsIdAndStatus(ids, "1");
                if (itemList.size() > 0) {
                    //searchService.importList(itemList);
                    //得到 itemlist 的 json 格式字符串
                    final String jsonString = JSON.toJSONString(itemList);

                    jmsTemplate.send(queueSolrDestination, new MessageCreator() {
                        @Override
                        public Message createMessage(Session session) throws JMSException {
                            return session.createTextMessage(jsonString);
                        }
                    });
                }
                System.out.println("--------------导入 activemq 中插入 solr 库数据成功 ---------------");
                //发送生成静态页面的信息
                for (Long goodsId : ids) {
                    jmsTemplate.send(topicPageDestination, new MessageCreator() {
                        @Override
                        public Message createMessage(Session session) throws JMSException {
                            //把 long 类型的 goodsid 转换成 string （jms 中的文本类型）类型传输
                            return session.createTextMessage(goodsId+"");
                        }
                    });
                }
            }
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed();
        }
    }

    @RequestMapping("/delete")
    public Result delete(final Long[] ids) {
        try {
            // 逻辑删除，设置删除字段为 1
            goodsManagementService.delete(ids);
            //把数组转换成列表，传递从 solr 库删除的信息
            jmsTemplate.send(queueSolrDeleteDestination, new MessageCreator() {
                @Override
                public Message createMessage(Session session) throws JMSException {
                    return session.createObjectMessage(ids);
                }
            });
            System.out.println("--------------导入 activemq 中删除 solr 库数据成功 ---------------");
            //传递删除商品详情页信息给 activemq
            jmsTemplate.send(topicPageDeleteDestination, new MessageCreator() {
                @Override
                public Message createMessage(Session session) throws JMSException {
                    //发送 object 类型的信息
                    return session.createObjectMessage(ids);
                }
            });
            System.out.println("--------------导入 activemq 中删除商品 sku 详情页数据成功 ---------------");
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed();
        }
    }

}
